// 学习资源相关API接口
import axios from 'axios'
import type { LearningResource as Resource, ResourceComment, ResourceSearchParams } from '../types/resource'
import type { Category as ResourceCategory } from '../types/index'

// 临时定义ResourcePagination类型
interface ResourcePagination {
  items: Resource[]
  currentPage: number
  total: number
  pageSize: number
}

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// 获取资源分类
export const getResourceCategories = async (): Promise<ResourceCategory[]> => {
  return await api.get('/resources/categories')
}

// 搜索学习资源
export const searchResources = async (params: ResourceSearchParams): Promise<ResourcePagination> => {
  return await api.get('/resources/search', { params })
}

// 获取热门资源
export const getHotResources = async (count: number = 10): Promise<Resource[]> => {
  return await api.get('/resources/hot', { params: { count } })
}

// 获取最新资源
export const getNewResources = async (count: number = 10): Promise<Resource[]> => {
  return await api.get('/resources/new', { params: { count } })
}

// 获取资源详情
export const getResourceDetail = async (resourceId: number): Promise<Resource> => {
  return await api.get(`/resources/${resourceId}`)
}

// 上传学习资源
export const uploadResource = async (data: FormData): Promise<Resource> => {
  return await api.post('/resources/upload', data, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 下载学习资源
export const downloadResource = async (resourceId: number): Promise<{ downloadUrl: string }> => {
  return await api.post(`/resources/${resourceId}/download`)
}

// 收藏/取消收藏资源
export const favoriteResource = async (resourceId: number): Promise<{ isFavorite: boolean }> => {
  return await api.post(`/resources/${resourceId}/favorite`)
}

// 获取我的收藏资源
export const getFavoriteResources = async (params: {
  page?: number
  pageSize?: number
}): Promise<ResourcePagination> => {
  return await api.get('/resources/favorites', { params })
}

// 获取资源评论
export const getResourceReviews = async (resourceId: number, params: {
  page?: number
  pageSize?: number
}): Promise<{ list: ResourceComment[], total: number }> => {
  return await api.get(`/resources/${resourceId}/reviews`, { params })
}

// 添加资源评论
export const addResourceReview = async (resourceId: number, data: {
  rating: number
  content: string
}): Promise<ResourceComment> => {
  return await api.post(`/resources/${resourceId}/reviews`, data)
}

// 管理员审核资源
export const approveResource = async (resourceId: number, approved: boolean, reason?: string): Promise<void> => {
  return await api.put(`/admin/resources/${resourceId}/approval`, { approved, reason })
}

// 管理员删除资源
export const deleteResource = async (resourceId: number): Promise<void> => {
  return await api.delete(`/admin/resources/${resourceId}`)
}